<!DOCTYPE html>
<html debug="true">
  <head>
    <link rel="stylesheet" type="text/css" href="../../../../ext/resources/css/ext-all.css" />

    <script type="text/javascript" src="../../../../openlayers/lib/Firebug/firebug.js"></script>
    <script type="text/javascript" src="../../../../openlayers/lib/OpenLayers.js"></script>

    <script type="text/javascript" src="../../../../ext/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="../../../../ext/ext-all-debug.js"></script>

    <script type="text/javascript">
      // Because of a bug in Firefox 2 we need to specify the MapFish base path.
      // See https://bugzilla.mozilla.org/show_bug.cgi?id=351282
      var gMfLocation = "../../../../mapfish/";
    </script>
    <script type="text/javascript" src="../../../../mapfish/MapFish.js"></script>

    <link rel="stylesheet" type="text/css" href="style.css" />

    <script type="text/javascript"><!--
        function test_constructor(t) {
            t.plan(1);
            // setup
            var mediator, store;
            store = new Ext.data.Store({
                reader: new mapfish.widgets.data.FeatureReader(
                    {}, [{name: "name", type: "string"}]
                )
            });
            // 1 test
            mediator = new mapfish.widgets.data.FeatureStoreMediator({
                store: store
            });
            t.ok(mediator.store == store,
                 "ctor correctly sets store");
        }

        function test_addFeatures(t) {
            t.plan(4);
            // setup
            var mediator, store, features, flip, filter;
            store = new Ext.data.Store({
                reader: new mapfish.widgets.data.FeatureReader(
                    {}, [{name: "name", type: "string"}]
                )
            });
            mediator = new mapfish.widgets.data.FeatureStoreMediator({
                store: store
            });
            function createFeatures() {
                return [
                    new OpenLayers.Feature.Vector(null, {
                        name: "foo"
                    }),
                    new OpenLayers.Feature.Vector()
                ];
            }
            // 2 tests
            features = createFeatures();
            mediator.addFeatures(features);
            t.eq(store.getCount(), 2,
                 "addFeatures adds correct number of features");
            var record = store.getById(features[0].id);
            t.ok(record.get('feature') == features[0],
                 'addFeatures correctly adds features');
            // 1 test
            features = createFeatures();
            mediator.addFeatures(features, {append: false});
            t.eq(store.getCount(), 2,
                 "addFeatures adds correct number of features");
            // 1 test
            flip = false;
            filter = function() { flip = !flip; return flip; };
            features = createFeatures();
            mediator.addFeatures(features, 
                {append: false, filter: filter});
            t.eq(store.getCount(), 1,
                 "addFeatures adds correct number of features " +
                 "with filter passed");
        }

        // there's a bug in Ext where Ext dereferences an undefined
        // value, the bug triggers with a grouping store and group
        // field when store.loadData is called with only one arg
        // or with false as the second arg. addFeatures works
        // around that bug, so that test_addFeatures_ExtBug passes
        //
        // prefix the function name with test_ if you want to
        // actually run the test
        /*
        function ExtBug(t) {
            t.plan(1);
            // setup
            var store, a;
            store = new Ext.data.GroupingStore({
                reader: new Ext.data.ArrayReader({id: 0}, [
                    {name: "name", mapping: 1},
                    {name: "occupation", mapping: 2}
                ]),
                groupField: "name"
            });
            a = [[1, 'Bill', 'Gardener'], [2, 'Ben', 'Horticulturalist']];
            store.loadData(a, false); // same effect with store.loadData(a)
            // 1 test
            t.eq(store.getCount(), 2,
                 "correct number of records loaded");
        }
        */

        function test_addFeatures_ExtBug(t) {
            t.plan(2);
            // setup
            var mediator, store, features, flip, filter;
            store = new Ext.data.GroupingStore({
                reader: new mapfish.widgets.data.FeatureReader(
                    {}, [{name: "name", type: "string"}]
                ),
                groupField: "name"
            });
            mediator = new mapfish.widgets.data.FeatureStoreMediator({
                store: store
            });
            function createFeatures() {
                return [
                    new OpenLayers.Feature.Vector(null, {
                        name: "foo"
                    }),
                    new OpenLayers.Feature.Vector()
                ];
            }
            // 2 tests
            features = createFeatures();
            // {append: false} makes Ext panic without the workaround
            // in FeatureStoreMediator.js, see test_ExtBug for making
            // Ext panic without MapFish involves
            mediator.addFeatures(features, {append: false});
            t.eq(store.getCount(), 2,
                 "addFeatures adds correct number of features");
            var record = store.getById(features[0].id);
            t.ok(record.get('feature') == features[0],
                 'addFeatures correctly adds features');
        }

        function test_removeFeatures(t) {
            t.plan(1);
            // setup
            var mediator, store, features;
            store = new Ext.data.Store({
                reader: new mapfish.widgets.data.FeatureReader(
                    {}, [{name: "name", type: "string"}]
                )
            });
            mediator = new mapfish.widgets.data.FeatureStoreMediator({
                store: store
            });
            function createFeatures() {
                return [
                    new OpenLayers.Feature.Vector(null, {
                        name: "foo"
                    }),
                    new OpenLayers.Feature.Vector()
                ];
            }
            // 1 test
            features = createFeatures();
            mediator.addFeatures(features);
            mediator.removeFeatures(features);
            t.eq(store.getCount(), 0,
                 'removesFeatures correctly removes features');
        }
    --></script>

  <body>
    <div id="map"></div>
  </body>
</html>
